mini.parse();

function GradeRuleHandler() {
};

GradeRuleHandler.instance = new GradeRuleHandler();

GradeRuleHandler.isQueryBtnExpended = false;

//获得全局的规则数据表格对象
GradeRuleHandler.gradeRuleGrid = mini.get("gradeRuleGrid");

//加载表格数据
GradeRuleHandler.gradeRuleGrid.load();

/**
 * 根据用户权限展示功能按钮
 */
GradeRuleHandler.loadToolbar = function() {
	var array = new Array("grade_rule_add", "grade_rule_edit", "grade_rule_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	
	for (var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
GradeRuleHandler.loadToolbar();

/**
 * 添加数据时，检验最高等级规则是否有效，若有效，则返回最高等级ID + 1，反之，则不允许再添加新等级规则。
 * 说明：等级规则ID为整形序列，不允许出现间断。数值越大，等级越高。添加等级规则时需严格按照等级规则执行操作。
 * @return {}
 */
GradeRuleHandler.getBgrTopData = function(){
	var bgrId = -1;
	
	$.ajax({
	        url: __root + "/getBgrTopData.do",
	        type: "POST",
	        async: false,
	        success: function (data) {
	        	if(data != ""){
	        		if(data.bgrState == 1){
	        			bgrId = (data.bgrId + 1);
	        		}
	        	}else{
					bgrId = 1;	        	
	        	}
	        }
	    });
	    
	return bgrId;
}

/**
 * 弹出添加规则窗口
 */
GradeRuleHandler.addGradeRule = function() {
	var bgrId = GradeRuleHandler.getBgrTopData();
	if(bgrId == -1){
		MiniuiTools.miniWarning("系统提示","当前最高等级资源尚未启用，无需继续添加！");
		return;
	}
	
	miniWin.afreshNewWin();
	miniWin.setParams(500, 380, "添加规则");
	miniWin.setOption("bodyStyle","padding:0px;");

	miniWin.load("gradeRuleForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new", newBgrId: bgrId};
        iframe.contentWindow.GradeRuleFormHandler.passData(data);
	},function(action){
		GradeRuleHandler.gradeRuleGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑规则窗口
 */
GradeRuleHandler.editGradeRule = function() {
	var rowData = GradeRuleHandler.gradeRuleGrid.getSelected();
	var rowsData = GradeRuleHandler.gradeRuleGrid.getSelecteds();
	
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(500, 380, "编辑规则[<font color='blue'>"	+ rowData.bgrName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
	
		miniWin.load("gradeRuleForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bgrId: rowData.bgrId};
            iframe.contentWindow.GradeRuleFormHandler.passData(data);
		},function(action){
			GradeRuleHandler.gradeRuleGrid.reload();
		});
		
		miniWin.show();
	}else if(rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 判断当前操作的等级规则ID是否属于最高等级
 * @param {} bgrId
 * @return {}
 */
GradeRuleHandler.isTopGradeRuleID = function(bgrId){
	var isTop = false;
	
	$.ajax({
	        url: __root + "/getBgrTopData.do",
	        type: "POST",
	        async: false,
	        success: function (data) {
	        	if(data.bgrId == bgrId){
	        		isTop = true;
	        	}
	        }
	    });
	    
	return isTop;
}

/**
 * 更新规则状态
 * @param {} state
 */
GradeRuleHandler.updateGradeRuleState = function(state) {
	if (!MiniuiTools.hasPurview("grade_rule_edit")) {
		return;
	}
	
	var rowData = GradeRuleHandler.gradeRuleGrid.getSelected();
	var bgrId = rowData.bgrId;	
	
	if(!GradeRuleHandler.isTopGradeRuleID(bgrId)){
		MiniuiTools.miniWarning("系统提示","您操作的不是最高等级资源，无法编辑等级状态！");
		return;
	}	
	
	GradeRuleHandler.gradeRuleGrid.loading();
	
    $.ajax({
        url: __root + "/updateGradeRuleStateById.do",
        data: {
        	bgrId: bgrId,
        	state: state,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            GradeRuleHandler.gradeRuleGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	GradeRuleHandler.gradeRuleGrid.unmask();
        }
    });	
};

/**
 * 执行查询规则的方法
 */
GradeRuleHandler.doQueryGradeRule = function(){
    GradeRuleHandler.gradeRuleGrid.load();
};

/**
 * 刷新页面数据及布局
 */
GradeRuleHandler.afresh = function(){
	GradeRuleHandler.doQueryGradeRule();
	mini.get("gradeRulePanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
GradeRuleHandler.gradeRuleGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

     if (column.field == "bgrIcon") {
        if (e.value != "") {
            e.cellHtml = "<img src='" + __root + "/imgViewerContorller.do?basePath=" + __realpath + __gradeIconPath + "&newImgPath=" + e.value + "'/>";                    
        } else {
            e.cellHtml = "";
        }
    }
    
     if (column.field == "bgrState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GradeRuleHandler.updateGradeRuleState(0)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GradeRuleHandler.updateGradeRuleState(1)'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
});

